<%@page language="java" pageEncoding="UTF-8" contentType="text/html;charset=utf-8"%>
<%@include file="/includes/taglibs.jsp"%>
<%@page import="com.feib.stms.action.BaseUploadActionBean"%>
<%@page import="com.feib.stms.util.StmsUtil"%>
<%@page import="com.feib.stms.model.LabelValue"%>

<%
// 欄位排序處理
BaseUploadActionBean actionBean = (BaseUploadActionBean) request.getAttribute("actionBean");
actionBean.setNavigator();
pageContext.setAttribute("actionBeanClassName", actionBean.getClass().getName());
%>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title></title>
        
    <script type="text/javascript">
    <!-- 
    $(document).ready(function() {
    	
    	$('#doUploadToTemporaryBtn').click(function() {
    		
    		if (''== $('#attachment').val()){
    			showErrorMessageDialog('請先選擇檔案！');
    			return false;
    		}
    		
    		return true;
    	});
    	

<c:if test="${null != actionBean.queryResult && not empty actionBean.queryResult.paginatedList}">
		$('#doUploadBtn').click(function() {
			var passed = false;
    		$('#confirmUploadFileForm input:checkbox.chk').each(function(){ 
    			if( $(this).attr("checked"))
    			{
    				passed = true;
    			}
    		});
    		
    		if (! passed)
    		{
    			showErrorMessageDialog('至少選一個檔案！');
    		}
    		else{
    			$('#messageDiv').dialog('destroy');
                $('#messageDiv').show();
                $('#messageDiv').html("是否確定上傳?");
                
                $("#messageDiv" ).dialog({
                title:'<span class="ui-icon ui-icon-info" style="float: left; margin-right: .3em;"></span><strong>訊息</strong>' 
                    , width: 300
                    , resizable: false
                    , modal: true        
                    , overlay: {
                        backgroundColor: '#000',
                        opacity: 0.9
                    }
                    , buttons: {
                        Ok: function() {
                            $( this ).dialog( "close" ); 
                           	$("#hidden\\.doUploadBtn").trigger('click');
                        },
                        Cancel: function() {
                            $( this ).dialog( "close" );
                          
                        }
                    }
                }).prev().addClass('ui-state-highlight');
    		}
    		
    		return passed;
				
		});
		
		$("[id^='doDeleteTempUploadFileBtn-']").click(function() {
			var tempUploadFilePk = this.id.substring(26);
			$('#hidden\\.tempUploadFilePk').val(tempUploadFilePk);
			
			$('#messageDiv').dialog('destroy');
		    $('#messageDiv').show();
		    $('#messageDiv').html("是否確定刪除 ?");
		    
		    $("#messageDiv" ).dialog({
		    	title:'<span class="ui-icon ui-icon-info" style="float: left; margin-right: .3em;"></span><strong>訊息</strong>' 
		        , width: 300
		        , resizable: false
		        , modal: true        
		        , overlay: {
		            backgroundColor: '#000',
		            opacity: 0.9
		        }
		        , buttons: {
		            Ok: function() {
		                $( this ).dialog( "close" );
						$("#hidden\\.doDeleteTempUploadFileBtn").trigger('click');
		            },
					Cancel: function() {
						$( this ).dialog( "close" );
					}
		        }
		    }).prev().addClass('ui-state-highlight');
			
		});
		
		$("[id^='doViewUploadErrorBtn-']").click(function() {
			var tempUploadFilePk = this.id.substring(21);
			$('#hidden\\.tempUploadFilePk').val(tempUploadFilePk);
			$("#hidden\\.doViewUploadErrorBtn").trigger('click');
		});
		
		$("[id^='viewDetail-']").click(function() {
				var tempUploadFilePk = this.id.substring(11);
				var ary = tempUploadFilePk.split('-');
				<c:if test="${actionBeanClassName == 'com.feib.stms.action.Stms2001ActionBean'}" var="result" />
 				var action=<c:out value="${result}"></c:out>;
								
			if(ary[0]==1 && action ){
				$('#hidden\\.fileFormat').val(ary[0]);
				$('#hidden\\.tempUploadFilePk').val(ary[1]);
				$("#hidden\\.doViewChkDetailBtn").trigger('click');
				
			}else{
				$('#hidden\\.fileFormat').val(ary[0]);
				$('#hidden\\.tempUploadFilePk').val(ary[1]);
				$("#hidden\\.doViewDetailBtn").trigger('click');
			}
						
		});
		
		$("[id^='a-sortHeader-']").click(function() {
			var sortField = this.id.substring(13);
			$('#hidden\\.queryResult\\.sortCriterion').val(sortField);
			
			var clazz = $(this).attr('class');
			if (null == clazz || 0 == clazz.length)
				$('#hidden\\.queryResult\\.sortDirection').val('1');
			else if ('sortAsc' == clazz)
				$('#hidden\\.queryResult\\.sortDirection').val('1');
			else
				$('#hidden\\.queryResult\\.sortDirection').val('2');
				
			$('#hidden\\.queryResult\\.pageNumber').val('1');
				
			$("#hidden\\.doQueryBtn").trigger('click');
			return false;
			
		});
		
		$("[id^='firstPageBtn']").click(function() {
			$('#hidden\\.queryResult\\.pageNumber').val('1');
			$("#hidden\\.doQueryBtn").trigger('click');
		});
		
		$("[id^='prePageBtn']").click(function() {
			$('#hidden\\.queryResult\\.pageNumber').val('<c:out value="${prePageNumber}"/>');
			$("#hidden\\.doQueryBtn").trigger('click');
		});
		
		$("[id^='nextPageBtn']").click(function() {
			$('#hidden\\.queryResult\\.pageNumber').val('<c:out value="${nextPageNumber}"/>');
			$("#hidden\\.doQueryBtn").trigger('click');
		});
		
		$("[id^='lastPageBtn']").click(function() {
			$('#hidden\\.queryResult\\.pageNumber').val('<c:out value="${lastPageNumber}"/>');
			$("#hidden\\.doQueryBtn").trigger('click');
		});
		
		$("[id^='currentPageText']").change(function() {
			var value = this.value;
			if (value != ''){
				value = value.replace(/^\s\s*/, '').replace(/\s\s*$/, '');
				var intRegex = /^\d+$/;
				if(!intRegex.test(value)) {
					showErrorMessageDialog('頁數必須是整數');
					return ;
				}
				
				value = parseInt(value, 10);
				
				if (value < 1){
					showErrorMessageDialog('頁數必須是大於1');
					return ;
				}
				else if (value > <c:out value='${lastPageNumber}'/>){
					showErrorMessageDialog('頁數必須是小於總頁數 (<c:out value="${lastPageNumber}"/>)');
					return ;
				}
				else if (value != <c:out value='${currentPageNumber}'/>){
					$('#hidden\\.queryResult\\.pageNumber').val(value);
					$("#hidden\\.doQueryBtn").trigger('click');
					return false;
				}
				
			}
			else{
				showErrorMessageDialog('頁數不可為空白)');
				return ;
			}
		});
</c:if>



<c:if test="${not empty actionBean.errorMessage}">
showErrorMessageDialog('<c:out value="${actionBean.errorMessage }" escapeXml="false"/>');
</c:if>

<c:if test="${not empty actionBean.message}">
showMessageDialog('<c:out value="${actionBean.message }" escapeXml="false"/>');
</c:if>


                 
    });
    
	function doConfirmAll()
	{
		$('#confirmUploadFileForm input:checkbox.chk').each(function(){              
	    	$(this).attr("checked", true);
	   });
	}
	
	function unConfirmAll()
	{
		$('#confirmUploadFileForm input:checkbox.chk').each(function(){               
	    	$(this).attr("checked", false);      	
	   });
	}
    
    //-->
    </script>
</head>
<body>

	<div id="smallQueryForm"></div>

<stripes:form id="uploadFileFrom" beanclass="<%=actionBean.getClass().getName() %>" method="post" onsubmit="javascript: waitingDialog({});">	
	<div id="queryForm">
		<table>
			<tr>
				<td colspan="4" class="queryHeader">請選擇上傳檔案</td>
			</tr>
			<tr>
				<th>上傳類別</th>
				<td colspan="4"><%=((LabelValue)StmsUtil.getInstance().getFileFormatMap().get(actionBean.getUploadFileFormat())).getLabel() %></td>
			</tr>
			<tr>
				<th>上傳檔案<span id="mustKeyInStar">**</span></th>
				<td>
					<stripes:file name="attachment" id="attachment" size="50"/> <span class="queryFunction"><stripes:submit id="doUploadToTemporaryBtn" name="doUploadToTemporary" value="加入上傳檔案清單"/></span>
				</td>
			</tr>
		</table>
	</div>
</stripes:form>
	

<stripes:form id="confirmUploadFileForm" beanclass="<%=actionBean.getClass().getName() %>" method="post" onsubmit="javascript: waitingDialog({});">
	<div id="listForm">
<c:if test="${null != actionBean.queryResult && not empty actionBean.queryResult.paginatedList}">
		<div id="paginationBar" style=" margin: 0 auto;">
			<input type="button" id="firstPageBtn" class="formBtn" value="第一頁" <c:out value='${prePageNumber<=0 ? "disabled": "" }'/> />
			<input type="button" id="prePageBtn" class="formBtn" value="上一頁" <c:out value='${prePageNumber<=0 ? "disabled": "" }'/> />
			第 <input type="text" id="currentPageText" class="formBtn" value="${currentPageNumber }" size="3" maxlength="4" /> / ${lastPageNumber} 頁
			<input type="button" id="nextPageBtn" class="formBtn" value="下一頁" <c:out value='${lastPageNumber<nextPageNumber ? "disabled": "" }'/> />
			<input type="button" id="lastPageBtn" class="formBtn" value="最末頁" <c:out value='${lastPageNumber<=currentPageNumber ? "disabled": "" }'/> />
		</div>
		
		<table>
			<thead>
			<tr>
				<td colspan="11" class="listHeader">上傳檔案清單 :</td>
			</tr>
			<tr>
				<th></th>
				<th><a id='a-sortHeader-uploadDate' href='javascript:void(0);' class="<c:out value='${uploadDateSortedClass }'/>"> <c:out value='${uploadDateSortedSpan }' escapeXml="false"/>上傳日期</a></th>
				<th><a id='a-sortHeader-fileFormat' href='javascript:void(0);' class="<c:out value='${fileFormatSortedClass }'/>"> <c:out value='${fileFormatSortedSpan }' escapeXml="false"/>上傳類別</a></th>
				<th><a id='a-sortHeader-fileName' href='javascript:void(0);' class="<c:out value='${fileNameSortedClass }'/>"> <c:out value='${fileNameSortedSpan }' escapeXml="false"/>檔案名稱</a></th>				
				<th><a id='a-sortHeader-totalNetIncomeCount' href='javascript:void(0);' class="<c:out value='${totalNetIncomeCountSortedClass }'/>"> <c:out value='${totalNetIncomeCountSortedSpan }' escapeXml="false"/>淨收筆數</a></th>
				<th><a id='a-sortHeader-totalNetIncome' href='javascript:void(0);' class="<c:out value='${totalNetIncomeSortedClass }'/>"> <c:out value='${totalNetIncomeSortedSpan }' escapeXml="false"/>淨收金額</a></th>
				<th><a id='a-sortHeader-totalNetPayCount' href='javascript:void(0);' class="<c:out value='${totalNetPayCountSortedClass }'/>"> <c:out value='${totalNetPayCountSortedSpan }' escapeXml="false"/>淨付筆數</a></th>
				<th><a id='a-sortHeader-totalNetPay' href='javascript:void(0);' class="<c:out value='${totalNetPaySortedClass }'/>"> <c:out value='${totalNetPaySortedSpan }' escapeXml="false"/>淨付金額</a></th>
				<th><a id='a-sortHeader-chksum' href='javascript:void(0);' class="<c:out value='${chksumSortedClass }'/>"> <c:out value='${chksumSortedSpan }' escapeXml="false"/>總檢核數</a></th>
				<th><a id='a-sortHeader-fileStatus' href='javascript:void(0);' class="<c:out value='${fileStatusSortedClass }'/>"> <c:out value='${fileStatusSortedSpan }' escapeXml="false"/>狀態</a></th>
				<th></th>		
			</tr>
			</thead>
			<tbody>
			<c:forEach items="${actionBean.queryResult.paginatedList}" var="viewObj" varStatus="rowstat">
			<tr>
				<td>
					<c:choose>
					<c:when test="${actionBeanClassName == 'com.feib.stms.action.Stms2001ActionBean' && viewObj.fileFormat == '1' }">
						<stripes:checkbox id="tempUploadFilePks[${ rowstat.index}]" name="tempUploadFilePks[${ rowstat.index}]" value="${viewObj.tempUploadFilePk}" class="chk"/>
					</c:when>
					<c:otherwise>
						<c:if test="${40==viewObj.fileStatus }">					
							<stripes:checkbox id="tempUploadFilePks[${ rowstat.index}]" name="tempUploadFilePks[${ rowstat.index}]" value="${viewObj.tempUploadFilePk}" class="chk"/>
						</c:if>
					</c:otherwise>
					</c:choose>
					<input type="hidden" id="tempUploadFileFormats[${ rowstat.index}]" name="tempUploadFileFormats[${ rowstat.index}]" value="${viewObj.fileFormat }"/>
				</td>
				<td align="center"><fmt:formatDate value="${viewObj.uploadDate}" pattern="yyyy/MM/dd HH:mm" /></td>
				<td><c:out value='${viewObj.fileFormatName }'/></td>
				<td><c:out value='${viewObj.fileName}'/></td>
				<td align="right"><fmt:formatNumber value="${viewObj.totalNetIncomeCount}" pattern="##,###" /></td>
				<td align="right"><fmt:formatNumber value="${viewObj.totalNetIncome}" pattern="##,###" /></td>
				<td align="right"><fmt:formatNumber value="${viewObj.totalNetPayCount}" pattern="##,###" /></td>
				<td align="right"><fmt:formatNumber value="${viewObj.totalNetPay}" pattern="##,###" /></td>
				<td><c:out value='${viewObj.formatedChksum }'/></td>
				<td><c:out value='${viewObj.fileStatusName }'/></td>
				<td width="10%">
					
					<c:choose>
					<c:when test="${actionBeanClassName == 'com.feib.stms.action.Stms2001ActionBean' && viewObj.fileFormat == '1' }">
						<div id="viewDetail-${viewObj.fileFormat}-<c:out value='${viewObj.tempUploadFilePk}'/>" class="widgetsIcon ui-state-default ui-corner-all" title="檢視"><span class="ui-icon ui-icon-circle-triangle-e"></span></div>
					</c:when>
					<c:otherwise>
						<c:choose>
							<c:when test="${viewObj.fileStatus == 10 || viewObj.fileStatus == 20 || viewObj.fileStatus == 30}">
								<div id="doViewUploadErrorBtn-${viewObj.tempUploadFilePk}" class="widgetsIcon ui-state-default ui-corner-all" title="檢視錯誤"><span class="ui-icon ui-icon-alert"></span></div>
							</c:when>
							<c:otherwise>
								<div id="viewDetail-${viewObj.fileFormat}-<c:out value='${viewObj.tempUploadFilePk}'/>" class="widgetsIcon ui-state-default ui-corner-all" title="檢視"><span class="ui-icon ui-icon-circle-triangle-e"></span></div>
							</c:otherwise>
						</c:choose>
						<div id="doDeleteTempUploadFileBtn-${viewObj.tempUploadFilePk}" class="widgetsIcon ui-state-default ui-corner-all" title="刪除"><span class="ui-icon ui-icon-trash"></span></div>
					</c:otherwise>
					</c:choose>
				</td>		
			</tr>
			</c:forEach>
			</tbody>
			<tfoot>
			<tr>
				<td colspan="11" class="listFunction">
					<input type="button" id="checkAll" name="checkAll" value="全選" onclick="javascript: doConfirmAll();"/>
					<input type="button" id="unCheckAll" name="unCheckAll" value="全不選" onclick="javascript: unConfirmAll();" class="formBtn" />
					<input type="button" id="doUploadBtn" value="上傳" name="doUpload" class="formBtn"/>
				</td>
			</tr>
			</tfoot>
		</table>
</c:if>
	</div>
	<div style="display: none;">
			<stripes:submit id="hidden.doUploadBtn" name="doUpload" value="上傳" class="formBtn"/>				
	</div>	
</stripes:form>

<div style="display: none;">
<stripes:form id="hidden.queryTempUploadFileFrom" beanclass="<%=actionBean.getClass().getName() %>" method="post" onsubmit="javascript: waitingDialog({});">
	
	<stripes:hidden id="hidden.queryResult.pageNumber" name="queryResult.pageNumber" />
	<stripes:hidden id="hidden.queryResult.sortCriterion" name="queryResult.sortCriterion" />
	<stripes:hidden id="hidden.queryResult.sortDirection" name="queryResult.sortDirection" />
	<stripes:hidden id="hidden.tempUploadFilePk" name="tempUploadFilePk" />
	<stripes:hidden id="hidden.fileFormat" name="fileFormat" />
	<stripes:submit id="hidden.doQueryBtn" name="doQuery" value="查詢" class="formBtn"/>
	<stripes:submit id="hidden.doDeleteTempUploadFileBtn" name="doDeleteTempUploadFile" value="刪除" class="formBtn"/>
	<stripes:submit id="hidden.doViewUploadErrorBtn" name="doViewUploadError" value="檢視錯誤" class="formBtn"/>
	<stripes:submit id="hidden.doViewDetailBtn" name="doViewDetail" value="檢視明細" class="formBtn"/>
	<stripes:submit id="hidden.doViewChkDetailBtn" name="doViewChkDetail" value="檢視驗資明細" class="formBtn"/>
	<stripes:errors></stripes:errors>
</stripes:form>
</div>
	
	
</body>
</html>
